﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace DSS_Simplex
{
    public partial class FormConst : Form
    {
        string MyConnectionString = "Server=localhost;Database=dss;Uid=root;Pwd=;";
        private string textFilter;
        private DataSet ds;
        FormConstPop formSearch = null;

        CheckBox chkbox = new CheckBox();
       
        public FormConst()
        {
            InitializeComponent();
            BindDatakeGrid();

        }
        private void BindDatakeGrid() 
        {
            ds = new DataSet();
            MySqlConnection connection = new MySqlConnection(MyConnectionString);
            connection.Open();
            try
            {
                MySqlCommand cmd = connection.CreateCommand();
                cmd.CommandText = "SELECT nama_const, min_const, max_const, equal FROM `constraint`";
                MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                da.Fill(ds);

                dataGridView1.DataSource = ds.Tables[0].DefaultView;
                
            }
            catch(Exception ex) 
            {
                MessageBox.Show(string.Format(ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        private void chkBoxChange(object sender, EventArgs e)
        {
            for (int k = 0; k <= dataGridView1.RowCount - 1; k++)
            {
                this.dataGridView1[0, k].Value = this.chkbox.Checked;
            }
            this.dataGridView1.EndEdit();
        }
        private void searchgrid_TextChanged(List<field> fields)
        {
            System.Data.DataView dv = ((System.Data.DataView)dataGridView1.DataSource);
            textFilter = ""; //reset

            bool first = true;
            foreach (field f in fields)
            {
                if (f.Value.Length > 0)
                {
                    if (!first) textFilter += " and ";
                    textFilter += f.Field + " like '%" + f.Value + "%'";
                    first = false;
                }
            }
            dv.RowFilter = textFilter;
            this.label1.Text = textFilter;
        }

        private void BtnReset_Click(object sender, EventArgs e)
        {
            formSearch = null;
            textFilter = "";
            this.label1.Text = "Filter: None";
            System.Data.DataView dv = ((System.Data.DataView)dataGridView1.DataSource);
            dv.RowFilter = textFilter;
        }

        private void BtnFilter_Click(object sender, EventArgs e)
        {
            if (formSearch == null)
            {
                List<field> fields = new List<field>();
                field f = new field();
                f.FriendlyName = "Const Name";
                f.Field = "nama_const";
                fields.Add(f);

                formSearch = new FormConstPop(fields);
                formSearch.searchgrid_TextChanged += new SearchContextChangedHandler(searchgrid_TextChanged);

            }
            formSearch.ShowDialog();
        }
        private void BtnAdd_Click(object sender, EventArgs e)
        {
            int i = 0;
            List<int> CheckedRow = new List<int>();

            for (i = 0; i <= dataGridView1.RowCount - 1; i++)
            {
                if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["chkcol"].Value) == true)
                {
                    CheckedRow.Add(i);
                }
            }
            foreach (int k in CheckedRow)
            {
                Form1 frm = new Form1();
            }
        }
    }
}
